<?php

use App\Models\Permission\ModuleName;
use App\Models\Permission\Permission;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddUserModulePermissions extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        $defaultAbilities = ['read', 'create', 'update', 'delete'];

        foreach ($defaultAbilities as $ability) {
            Permission::create([
                'module_name' => ModuleName::user,
                'feature_name' => 'user',
                'ability' => $ability,
            ]);
        }
        foreach ($defaultAbilities as $ability) {
            Permission::create([
                'module_name' => ModuleName::user,
                'feature_name' => 'role',
                'ability' => $ability,
            ]);
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() {
        Permission::where([
            'module_name' => 'user',
            'feature_name' => 'role',
        ])->delete();
        Permission::where([
            'module_name' => 'user',
            'feature_name' => 'role',
        ])->delete();
    }
}
